#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int editDistanceDP(string s, string t) {
  int s_length = s.length();
  int t_length = t.length();
  vector<vector<int>> dp(s_length + 1, vector<int>(t_length + 1, 0));
  for (int i = 1; i <= s_length; i++) {
    dp[i][0] = i;
  }
  for (int i = 1; i <= t_length; i++) {
    dp[0][i] = i;
  }
  for (int i = 1; i <= s_length; i++) {
    for (int j = 1; j <= t_length; j++) {
      // 当前字符相同
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        // 最少编辑步骤 = min(插入、删除、替换) + 1
        dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
      }
    }
  }
  return dp[s_length][t_length];
}

int editDistanceDP_space(string s, string t) {
  int s_length = s.length();
  int t_length = t.length();
  vector<int> dp(t_length + 1, 0);
  for (int i = 0; i <= s_length; i++) {
    dp[i] = i;
  }
  for (int i = 1; i <= s_length; i++) {
    int leftUp = dp[0];
    for (int j = 1; j <= t_length; j++) {
      int temp = dp[j];
      // 当前字符相同
      if (s[i - 1] == t[j - 1]) {
        dp[j] = temp;
      } else {
        // 最少编辑步骤 = min(插入、删除、替换) + 1
        dp[j] = min(min(dp[j], dp[j - 1]), leftUp) + 1;
      }
      leftUp = temp;
    }
  }
  return dp[t_length];
}

int main() {
  string s{"hello"};
  string t{"algo"};
  int nums = editDistanceDP_space(s, t);
  cout << nums << endl;
}